package com.yunquan.ohana.ui.history.list

import android.os.Bundle
import androidx.databinding.ObservableField
import com.yunquan.ohana.R
import com.yunquan.ohana.base.binding.command.BindingAction
import com.yunquan.ohana.base.binding.command.BindingCommand
import com.yunquan.ohana.base.ui.ItemViewModel
import com.yunquan.ohana.entity.FileGroupEntity
import com.yunquan.ohana.ui.history.details.DetailsActivity
import com.yunquan.ohana.ui.history.status.StatusActivity
import com.yunquan.ohana.utils.StringUtils
import com.yunquan.ohana.utils.ToastUtils

class HistoryItemViewModel(viewModel: HistoryListViewModel) : ItemViewModel<HistoryListViewModel>(viewModel) {

    var data: FileGroupEntity? = null

    val frameName = ObservableField<String>()
    val content = ObservableField<String>()
    val progress = ObservableField<Int>()
    val scale = ObservableField<String>()
    val coverPath = ObservableField<String>()
    val countText = ObservableField<String>()
    val showCount = ObservableField<Boolean>()
    val showLookStatus = ObservableField<Boolean>()
    val showPlay = ObservableField<Boolean>()
    val showBtnRevoke = ObservableField<Boolean>()
    val showBtnSend = ObservableField<Boolean>()

    val showProgress = ObservableField<Boolean>()

    constructor(viewModel: HistoryListViewModel, data: FileGroupEntity) : this(viewModel) {
        this.data = data
        if (data.frames.isNotEmpty()) {
            if (data.frames.size == 1) {
                frameName.set(data.frames[0].name)
            } else {
                frameName.set("${data.frames[0].name}.etc")
            }
        }
        val msg: String? = try {
            data.photos.first { !StringUtils.isEmpty(it.content) }.content
        } catch (_: Exception) {
            null
        }
        content.set(msg)
        progress.set(0)
        scale.set("0%send")
        if (data.photos.isNotEmpty()) {
            if (data.photos[0].type == 2) {
                coverPath.set(data.photos[0].thumbnail ?: data.photos[0].videoImgUrl ?: data.photos[0].localFilePath)
                showPlay.set(true)
            } else {
                coverPath.set(data.photos[0].localCropPath ?: data.photos[0].localFilePath ?: data.photos[0].fileUrl)
                showPlay.set(false)
            }
        }
        countText.set(if (data.photos.size > 1) "${data.photos.size}+" else "")
        showCount.set(data.photos.size > 1)
        val framesStatus = try {
            if (viewModel.showType.get() == "Completed") {
                data.frames.first { it.receipt == false }
            } else {
                null
            }
        } catch (_: Exception) {
            null
        }
        showLookStatus.set(framesStatus != null)
        showBtnRevoke.set(viewModel.showType.get() == "Sending" || viewModel.showType.get() == "Completed" || viewModel.showType.get() == "Failed")
        showBtnSend.set(viewModel.showType.get() == "Drafts" || viewModel.showType.get() == "Completed" || viewModel.showType.get() == "Failed")
        showProgress.set(viewModel.showType.get() == "Sending")
    }

    fun setProgress(progress: Int) {
        this.progress.set(progress)
        this.scale.set("${progress}%send")
    }

    // 查看接收状态
    val onItemClick = BindingCommand<Any>(object : BindingAction {
        override fun call() {
            val bundle = Bundle()
            bundle.putSerializable("data", data!!)
            viewModel.startActivity(DetailsActivity::class.java, bundle)
        }
    })

    // 查看接收状态
    val onLookClick = BindingCommand<Any>(object : BindingAction {
        override fun call() {
            if (data?.serviceGroupId != null) {
                val bundle = Bundle()
                bundle.putSerializable("groupUid", data!!.serviceGroupId)
                viewModel.startActivity(StatusActivity::class.java, bundle)
            }
        }
    })

    // 发送
    val onSendClick = BindingCommand<Any>(object : BindingAction {
        override fun call() {
            if (data == null) {
                ToastUtils.showShort(R.string.incorrect_data)
                return
            }
            viewModel.onResend(this@HistoryItemViewModel)
        }
    })

    // 撤回
    val onRevokeClick = BindingCommand<Any>(object : BindingAction {
        override fun call() {
            viewModel.onRevoke(this@HistoryItemViewModel)
        }
    })

    // 删除
    val onDeleteClick = BindingCommand<Any>(object : BindingAction {
        override fun call() {
            viewModel.onDelete(this@HistoryItemViewModel)
        }
    })
}